//
//  MooString.cpp
//  BattleEngine
//
//  Created by machine on 12-11-1.
//  Copyright (c) 2012年 machine. All rights reserved.
//

#include "MooString.h"

void test() {
    string s = "12345";
    s = MooString::md5(MooString::toUpper(s));

    cout << "---" << s << "---" << endl;
}

string MooString::toUpper(const string & str) {
    string tmp = "";
    for (string::const_iterator i = str.begin(); i < str.end(); i++) {
        tmp += toupper(*i);
    }
    
    return tmp;
}

string MooString::toLower(const string & str) {
    string tmp = "";
    for (string::const_iterator i = str.begin(); i < str.end(); i++) {
        tmp += tolower(*i);
    }
    
    return tmp;
}

string MooString::trim(const string & str) {
    return MooString::ltrim(MooString::rtrim(str));
}

string MooString::rtrim(const string & str) {
    int index = 0;
    
    for (string::const_iterator i = str.end() - 1; i != str.begin(); i--) {
        if (int(*i) != 32) {
            break;
        }
        index++;
    }
    
    return str.substr(0, str.length() - index);
}

string MooString::ltrim(const string & str) {
    int index = 0;

    for (string::const_iterator i = str.begin(); i != str.end(); i++) {
        if (int(*i) != 32) {
            break;
        }
        index++;
    }
    
    return str.substr(index, str.length() - index);
}


string::size_type MooString::indexOf(const string &str, const string &search) {
    string::size_type strlen = str.length();
    string::size_type sealen = str.length();
    
    if (sealen > strlen) {
        return -1;
    }
    
    string tmp;
    for (string::size_type i = 0; i < strlen; i++) {
        if (i + sealen > strlen) {
            return -1;
        }
        
        // 逐个字母进行对比
        tmp = "";
        for (int j = 0; j < sealen; j++) {
            tmp += str[i + j];
        }
        if (tmp == search) {
            return i;
        }
    }
    
    return -1;
}

vector<string> MooString::explode(const string &_str, const string & str) {
    vector<string> rs;
    
    string::size_type _strlen = _str.length();
    string::size_type strlen = str.length();
    
    if (strlen > _strlen) {
        rs.push_back(_str);
        return rs;
    }
    
    string tmp;
    string::size_type lastIndex = 0;
    for (string::size_type i = 0; i < _strlen; i++) {
        if (i + strlen > _strlen) {
            break;
        }
        
        // 逐个字母进行对比
        tmp = "";
        for (int j = 0; j < strlen; j++) {
            tmp += _str[i + j];
        }
        // 如果找到匹配的字符串，则把该字符串到上次查找位置之间的字符串截取出来
        if (tmp == str) {
            if (i == 0) {
                rs.push_back("");
            } else {
                rs.push_back(_str.substr(lastIndex, i - lastIndex));
            }
            lastIndex = i + strlen;
        }
    }
    
    if (lastIndex != 0) {
        rs.push_back(_str.substr(lastIndex, _strlen - lastIndex));
    } else {
        rs.push_back(_str);
    }
    return rs;
}

string MooString::replace(const string &_str, const string &search, const string &replace) {
    vector<string> rs = MooString::explode(_str, search);
    vector<string>::size_type size = rs.size();
    if (size == 1) {
        return _str;
    }
    
    string tmp = "";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
    int index = 0;
    for (index = 0; index <= size - 2; index++) {
        tmp += rs[index] + replace;
    }
    tmp += rs[index];
    
    return tmp;
}

string MooString::shuffle(const string &_str) {
    string str = _str;
    string rs = "";

    // 随机取出字母
    while (str.length() > 0) {
        long len = str.length();
        string::size_type index = MooNumber::rand(0, len - 1).toInt();
        rs += str[index];
        str.erase(str.begin() + index);
    }
    return rs;
}

string MooString::md5(const string &str) {
    char *s = new char[str.length()];
    strcpy(s, str.c_str());
    return string(MD5String(s));
}

